--[[--
类名: OptionMenu.lua
创建日期: 2015-4-9
创建人: 陈小虎
功能描述: 选项按钮集合
修改内容：
修改日期:   
修改人:     
修改备注: 
]]

local Vector = require("app.utils.Vector")

local OptionMenu = class("OptionMenu")

function OptionMenu:ctor()
    self.mOptionVec = Vector.new()
end

function OptionMenu:getSize()
    return self.mOptionVec:size()
end

function OptionMenu:addCheckBox(checkBox)
    checkBox:setSwallowTouches(false)
    self.mOptionVec:add(checkBox)
    local count = self.mOptionVec:size()
    self:resetListener(count)

    if count == 1 then
        self:switchOption(count)
    end

    self:switchOptionStatus(1)
end

function OptionMenu:addCheckBoxNotSelected(checkBox)
    self.mOptionVec:add(checkBox)
    local count = self.mOptionVec:size()
    self:resetListener(count)
    self:switchOptionStatus(0)
end

function OptionMenu:closeAllCheckBoxSelectedStatus()
    for i = 1, self.mOptionVec:size() do
        local checkBox = self.mOptionVec[i]
        checkBox:setSelected(false)
    end
end

function OptionMenu:removeCheckBoxByName(name)
    for i = 1, self.mOptionVec:size() do
        local checkBox = self.mOptionVec[i]
        if checkBox:getName() == name then
            self.mOptionVec:removeAt(i)
            break
        end
    end
end

function OptionMenu:removeAllCheckBox()
    for i =self.mOptionVec:size(),1,-1 do
        self.mOptionVec:removeAt(i)
    end
end

function OptionMenu:getCheckBoxByIndex(index)
    return self.mOptionVec:get(index)
end

function OptionMenu:getCheckBoxIndexByTag(tag)
    for i = 1, self.mOptionVec:size() do
        local btn = self.mOptionVec[i]
        if btn:getTag() == tag then
            return i
        end
    end
    return nil
end

function OptionMenu:setCallback(callback)
    self.mCallback = callback
end

function OptionMenu:setUnSelectCallback(callback)
    self.mUnSelectCallback = callback
end

function OptionMenu:setCallback1(callback1)
    self.mCallback1 = callback1
end

--function OptionMenu:select(index)
--    local btn = self.mOptionVec[index]
--    btn:setSelected(true)
--    self:resetListener()
--end

function OptionMenu:resetListener(index)
    local function selectedStateEvent(sender,eventType)
        if eventType == ccui.CheckBoxEventType.selected then
            print("----------------selected index:"..index)
            self:switchOption(index)
            -- self:switchOption(index)
        elseif eventType == ccui.CheckBoxEventType.unselected then
            print("-------------------unselected index:"..index)
            local isRealSelect = true
            if self.mCallback ~= nil then
                isRealSelect = self.mCallback(sender:getTag(), false,sender)
            end
            if (isRealSelect == nil) or isRealSelect then
                --没有返回，默认是选中
                sender:setSelected(true)
            end
            --如果设置不选中时取消高亮，则不显示选中
            if self.canUnselected then
                sender:setSelected(false)
            end
        end
    end

    local btn = self.mOptionVec[index]
    btn:addEventListener(selectedStateEvent)
    btn:setSelected(false)

    if self.mUnSelectCallback then
        self.mUnSelectCallback(btn:getTag())
    end
end

function OptionMenu:switchOptionStatus(index)
    for i = 1, self.mOptionVec:size() do
        local checkBox = self.mOptionVec[i]
        if checkBox then
            local nameNormalNode = checkBox:getChildByName("02Name_normal")
            local nameSelectedNode = checkBox:getChildByName("02Name_selected")
            local arrowNode = checkBox:getChildByName("04Icon_arrow")
            local function setStatus(visible)
                if nameNormalNode then nameNormalNode:setVisible(not visible) end
                if nameSelectedNode then nameSelectedNode:setVisible(visible) end
                if arrowNode then arrowNode:setVisible(visible) end
            end

            if index ~= i then
                setStatus(false)
            else
                setStatus(true)
            end
        end
    end
end

function OptionMenu:getOptionSize()
    return self.mOptionVec:size()
end

function OptionMenu:switchOption(index, isNotCallback)
    local function switchOption(button)
        if not button:isEnabled() then
            --如果按钮设置了disenabled，但是那么点击也应该是不选中
            -- button:setSelected(false)
            return
        end

        -- local selectBtn = nil
        -- for i = 1, self.mOptionVec:size() do
        --     local btn = self.mOptionVec[i]

        --     if btn ~= button then
        --         btn:setSelected(false)
        --         if self.mUnSelectCallback then
        --             self.mUnSelectCallback(btn:getTag())
        --         end
        --     else
        --         selectBtn = btn
        --         self:switchOptionStatus(i)
        --     end
        -- end

        local selectBtn = button

        local isRealSelect = true

        if selectBtn then
            if not isNotCallback then
                if self.mCallback ~= nil then
                    isRealSelect = self.mCallback(selectBtn:getTag(), true,selectBtn)
                end
                if self.mCallback1 then
                    self.mCallback1(selectBtn:getName())
                end
            end

            --如果有回调，从回调中判断是否真的可以选中
            if isRealSelect == nil or isRealSelect then
                selectBtn:setSelected(true)
            else
                selectBtn:setSelected(false)
            end
        end

        if isRealSelect == nil or isRealSelect then
            for i = 1, self.mOptionVec:size() do
                local btn = self.mOptionVec[i]

                if btn ~= button then
                    btn:setSelected(false)
                    if self.mUnSelectCallback then
                        self.mUnSelectCallback(btn:getTag())
                    end
                else
                    self:switchOptionStatus(i)
                end
            end
        end
    end

    local selIndex = 1
    if index then
        selIndex = index
    end

    local count = self.mOptionVec:size()
    if selIndex <= count then
        local btn = self.mOptionVec[selIndex]
        switchOption(btn)
    end
end

function OptionMenu:unselectAllOption()
    for i = 1, self.mOptionVec:size() do
        local btn = self.mOptionVec[i]
        btn:setSelected(false)
        if self.mUnSelectCallback then
            self.mUnSelectCallback(btn:getTag())
        end
    end
end

--设置不选中时取消高亮（选中的显示）
function OptionMenu:setCanUnselected(s)
    self.canUnselected=s
end

return OptionMenu